package com.zxc.item.controller;

import com.zxc.item.entity.TbCategory;
import com.zxc.item.service.TbCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class TbCategoryController {

    @Autowired
    private TbCategoryService tbCategoryService;

    /*
    * 直接通过商品微服务访问http://localhost:7000/category/list/1
    * 通过网关访问:http://localhost:9000/api/item/category/list/1
    * 通过域名访问:？？？？http://api.shopping.com/api/item/category/list/1
    * */
    @RequestMapping("/category/list/{parentId}")
    public ResponseEntity<List<TbCategory>> getCategory(@PathVariable(name = "parentId")Long parentId){
        List<TbCategory> categoryList = null;

        //如果请求的parentId为0，那么我们返回所有的类目列表
        if (parentId == 0){
            categoryList = tbCategoryService.getAllCategory();
        }else {
            categoryList = tbCategoryService.getCategoryByParentId(parentId);
        }

        //return ResponseEntity.status(HttpStatus.OK).body(categoryList);
        return ResponseEntity.ok(categoryList);
    }
}
